/**
 * 
 */
package neptune.web.client.view;

import com.google.gwt.user.client.ui.DeckPanel;

/**
 * A View that can contain other Views. The views container is realized through
 * a DeckPanel, that can show on command a particular widget.
 * 
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class SuperView extends View {

	DeckPanel mainPanel = new DeckPanel();

	/**
	 * Sets the view container. This method is useful to set the view container
	 * according to the SuperView layout
	 * 
	 * @param container
	 */
	public void setViewContainer(DeckPanel container) {
		this.mainPanel = container;
	}

	/**
	 * Adds a View to this SuperView, returns the index of the view inside the
	 * DeckPanel
	 * 
	 * @param view
	 * @return
	 * @see neptune.web.client.view.View
	 */
	public int addView(View view) {
		if (view.getName().equals(""))
			return -1;

		view.setPrefix(this.getToken());
		mainPanel.add(view);

		return mainPanel.getWidgetIndex(view);
	}

	/**
	 * Removes a View from this SuperView
	 * 
	 * @param view
	 * @see neptune.web.client.view.View
	 */
	public void removeView(View view) {
		View removingView = (View) mainPanel.getWidget(mainPanel
				.getWidgetIndex(view));
		removingView.setPrefix("");
		mainPanel.remove(view);
		HistoryTokenUtils.unregisterViewToken(removingView.getName());
	}

	/**
	 * Removes a View given its name. The view name is the history token, set
	 * with the setToken method
	 * 
	 * @param viewToken
	 * @see neptune.web.client.view.View
	 */
	public void removeView(String viewToken) {
		for (int i = 0; i < mainPanel.getWidgetCount(); i++) {
			View actualView = (View) mainPanel.getWidget(i);
			if (actualView.getName().equals(viewToken))
				mainPanel.remove(i);
		}
	}

	/**
	 * Set the active View given its token (prefix + name). The active View is
	 * the one that is showed to the user
	 * 
	 * @param viewToken
	 * @see neptune.web.client.view.View
	 */
	public void setActiveView(String viewToken) {

		String state = HistoryTokenUtils.getStateString(viewToken);
		String[] viewLevels = HistoryTokenUtils.getTokenHierarchy(viewToken);
		String thisLevel = viewLevels[0];
		String newToken = "";

		for (int i = 1; i < viewLevels.length; i++) {
			newToken = HistoryTokenUtils.buildToken(newToken, viewLevels[i]);
		}
		if (state != null && !newToken.equals(""))
			newToken = HistoryTokenUtils.addStateToToken(newToken, state);

		for (int i = 0; i < mainPanel.getWidgetCount(); i++) {
			View actualView = (View) mainPanel.getWidget(i);
			if (actualView.getName().equals(thisLevel)) {
				mainPanel.showWidget(i);
				if (!newToken.equals("")) {
					((SuperView) actualView).setActiveView(newToken);
				} else if (state != null) {
					setState(state);
				}
			}
		}
	}

	/**
	 * Gets the actual active View
	 * 
	 * @return
	 * @see neptune.web.client.view.View
	 */
	public View getActiveView() {
		int index = mainPanel.getVisibleWidget();
		if (index < 0)
			return null;
		return (View) mainPanel.getWidget(index);
	}

	/**
	 * Gets the view from it's name. Returns null if no views with such token
	 * are registered on this superView.
	 * 
	 * @param viewName
	 * @return
	 */
	public View getViewByName(String viewName) {
		for (int i = 0; i < mainPanel.getWidgetCount(); i++) {
			View actualView = (View) mainPanel.getWidget(i);
			if (actualView.getName().equals(viewName))
				return actualView;
		}
		return null;
	}

}
